package com.csun.dsms.controller;

import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.annotation.CurrentUser;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.model.domain.DsmsArea;
import com.csun.dsms.model.domain.DsmsUnit;
import com.csun.dsms.model.domain.DsmsUser;
import com.csun.dsms.model.vo.AreaUnitVo;
import com.csun.dsms.model.vo.menu.MenuVo;
import com.csun.dsms.service.DsmsAreaService;
import com.csun.dsms.service.DsmsAreaUnitService;
import com.csun.dsms.utils.PublicUtil;
import com.csun.dsms.utils.TreeUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/area")
public class DsmsAreaController {

    @Resource
    private DsmsAreaService dsmsAreaServiceImp;


    /*
    * 片区的添加
    * */
    @Authorization
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Wrapper areaAdd (@CurrentUser DsmsUser login, @RequestBody DsmsArea dsmsArea){
        /*
        * 判断片区名是否重复
        * */
        if(PublicUtil.isEmpty(dsmsArea.getName().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"片区名为空");
        }
        DsmsArea area = new DsmsArea();
        area.setName(dsmsArea.getName());
        List<AreaUnitVo> query = dsmsAreaServiceImp.query(area);
        if(query.size()!=0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"片区名已存在");
        }
        area.setName("");
        area.setId(dsmsArea.getPid());
        query = dsmsAreaServiceImp.query(area);
        if(query.get(0).getType() != null && query.get(0).getType() == 1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"无法在网格下新增子目录");
        }
        dsmsArea.setCreator(login.getLoginName());
        dsmsArea.setCreatorId(login.getId());
        dsmsArea.setLastOperator(login.getLoginName());
        dsmsArea.setLastOperatorId(login.getId());
        Integer save = dsmsAreaServiceImp.save(dsmsArea);
        if(dsmsArea.getUnitIds().size()>0){
            dsmsAreaServiceImp.areaAdd(dsmsArea);
        }
        if(save<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * 片区的删除
     * */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Wrapper areaDelete (@RequestBody DsmsArea dsmsArea){
        //根片区无法删除
        if(dsmsArea.getPid()!=null&&dsmsArea.getPid()==0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"该片区无法删除");
        }
        DsmsArea area = new DsmsArea();
        area.setPid(dsmsArea.getId());
        List<AreaUnitVo> query = dsmsAreaServiceImp.query(area);
        if(query.size()!=0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"该片区下存在分区");
        }
        Integer i = dsmsAreaServiceImp.deleteArea(dsmsArea);
        //删除该片区与单位绑定关系
        dsmsAreaServiceImp.deleteAreaUnit(dsmsArea);
        if(i<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * 片区的修改
     * */
    @Authorization
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Wrapper areaUpdate (@CurrentUser DsmsUser login, @RequestBody DsmsArea dsmsArea){
        dsmsArea.setLastOperator(login.getLoginName());
        dsmsArea.setLastOperatorId(login.getId());
        dsmsArea.setUpdateTime(new Date());
        /*
         * 判断片区名是否重复
         * */
        if(PublicUtil.isEmpty(dsmsArea.getName().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"片区名为空");
        }
        DsmsArea area = new DsmsArea();
        area.setName(dsmsArea.getName());
        List<AreaUnitVo> query = dsmsAreaServiceImp.query(area);
        if(query.size()!=0){
            for (AreaUnitVo areaUnitVo:query) {
                if(!areaUnitVo.getId().equals(dsmsArea.getId())){
                    return WrapMapper.wrap(Wrapper.ERROR_CODE,"片区名已存在");
                }
            }
        }
        Integer i = dsmsAreaServiceImp.updateArea(dsmsArea);
        if(i<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * 片区的查询
     * */
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Wrapper areaQuery (@RequestBody DsmsArea dsmsArea){
        List<AreaUnitVo> areaUnitVos = dsmsAreaServiceImp.query(dsmsArea);
        if(areaUnitVos.size() == 0){
            DsmsArea area = new DsmsArea();
            area.setId(dsmsArea.getPid());
            area.setName(dsmsArea.getName());
            List<AreaUnitVo> query = dsmsAreaServiceImp.query(area);
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(query));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(areaUnitVos));
    }

    /*
     * 片区菜单栏
     * */
    @RequestMapping(value = "/tree",method = RequestMethod.POST)
    public Wrapper areaQuery (){
        List<AreaUnitVo> query = dsmsAreaServiceImp.query(null);
        List<MenuVo> menuVos = new ArrayList<>();
        for (AreaUnitVo dsmsUnit:query) {
            MenuVo menuVo = new MenuVo();
            BeanUtils.copyProperties(dsmsUnit,menuVo);
            menuVo.setMenuName(dsmsUnit.getName());
            menuVos.add(menuVo);
        }
        List<MenuVo> childMenuVos = TreeUtil.getChildMenuVos(menuVos, 0L);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,childMenuVos);
    }

    /*
    * 获取所有片区
    * */
    @RequestMapping(value = "/getAllArea",method = RequestMethod.POST)
    public Wrapper getAllArea (){
        List<AreaUnitVo> areaUnitVos = dsmsAreaServiceImp.getAllArea();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(areaUnitVos));
    }

}
